//
//  YTEditMineController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/23.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTEditMineController.h"
#import "YTSexView.h"
#import "YTbirthdayView.h"

#define kNoEditColor [UIColor colorWithRed:24.0f/255.0f green:169.0f/255.0f blue:177.0f/255.0f alpha:1]
#define kEditColor [UIColor colorWithRed:165.0f/255.0f green:176.0f/255.0f blue:177.0f/255.0f alpha:1]

@interface YTEditMineController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *NameLabel;
@property (weak, nonatomic) IBOutlet UILabel *sexLabel;
@property (weak, nonatomic) IBOutlet UILabel *birthdayLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@property (weak, nonatomic) IBOutlet UIImageView *iconRadiusView;

@end

@implementation YTEditMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:136.0f/255.0f
                                                                          green:145.0f/255.0f
                                                                           blue:146.0f/255.0f
                                                                          alpha:1]];
    
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"icon_logo"]];
  
    [self setupUI];
   
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadUserInfo];

}
- (void)setupUI
{
    
    NSLog(@"%@",YTNetworkManager.shareManager.account.user_nicename);
    self.NameLabel.text = YTNetworkManager.shareManager.account.display_name;
    self.birthdayLabel.text = YTNetworkManager.shareManager.account.birthday;
    self.desLabel.text = YTNetworkManager.shareManager.account.decs;
    self.phoneLabel.text = YTNetworkManager.shareManager.account.user_phone;
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:YTNetworkManager.shareManager.account.user_avatar]];
    self.sexLabel.text = (kUserAccount.sex.intValue == 0) ? @"待完善" : ((kUserAccount.sex.intValue == 1) ? @"男" : @"女");
    
    [self.sexLabel setTextColor:(kUserAccount.sex.intValue == 0) ? kNoEditColor : kEditColor];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    
}

// 返回按钮
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}


- (void)loadUserInfo
{
    [YTNetworkManager.shareManager requestUserInfoWithCompletion:^(BOOL isSucc, NSString *statusInfo) {
        if (!isSucc) {
            [self.view showTipWithTipStr:statusInfo];
            return ;
        }
        
        [self setupUI];
    }];
}

#pragma mark: - 更换头像 监听
- (void)changeIcon
{
    //自定义消息框
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    sheet.tag = 2550;
    //显示消息框
    [sheet showInView:self.view];
}

#pragma mark -消息框代理实现-
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (actionSheet.tag == 2550) {
        NSUInteger sourceType = 0;
        // 判断系统是否支持相机
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            imagePickerController.delegate = self; //设置代理
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = sourceType; //图片来源
            if (buttonIndex == 2) {
                return;
            }else if (buttonIndex == 0) {
                //拍照
                sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePickerController.sourceType = sourceType;
                [self presentViewController:imagePickerController animated:YES completion:nil];
            }else if (buttonIndex == 1){
                //相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagePickerController.sourceType = sourceType;
                [self presentViewController:imagePickerController animated:YES completion:nil];
            }
        }else {
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePickerController.sourceType = sourceType;
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }
    }
}

#pragma mark -实现图片选择器代理-（上传图片的网络请求也是在这个方法里面进行，这里我不再介绍具体怎么上传图片）
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //通过key值获取到图片
    NSLog(@"%@",image);  //给UIimageView赋值已经选择的相片
    
    [YTNetworkManager.shareManager postAvatorWithImage:image
                                            completion:^(BOOL isSucc, NSString *statusInfo)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:statusInfo];
            return ;
        }
        
        [self.view showTipWithTipStr:statusInfo];
        
        [self loadUserInfo];
    }];
    //上传图片到服务器--在这里进行图片上传的网络请求，这里不再介绍
   
}

#pragma mark: - table view delegate
- (void)tableView:(UITableView *)tableView
                            didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row)
    {
        case 0: // 更换头像
            [self changeIcon];
            break;
        case 1: // 修改昵称
        {
            
            TYAlertView *view = [TYAlertView alertViewWithTitle:@"修改昵称" message:nil];
            
            __weak typeof(view) WeakView = view;
            view.titleLable.font = [UIFont systemFontOfSize:14];
        
    
            [view addAction:[TYAlertAction actionWithTitle:@"取消"
                                                     style:TYAlertActionStyleCancel
                                                   handler:^(TYAlertAction *action)
            {
                
            }]];
            
            [view addAction:[TYAlertAction actionWithTitle:@"确定"
                                                     style:TYAlertActionStyleDestructive
                                                   handler:^(TYAlertAction *action)
            {
                [self postEditInfoWithIdx:indexPath value:[WeakView.textFieldArray.firstObject text]];
            }]];
            
            [view addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.placeholder = @"请输入昵称";
            }];
            
//            // first way to show
            TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];

            [self presentViewController:alertController animated:YES completion:nil];
        }
           
            break;
        case 2: // 设置性别
        {
            YTSexView *sexView = [YTSexView createViewFromNib];
            
            sexView.selectedSexBlock = ^(NSInteger tag) {
                [self postEditInfoWithIdx:indexPath value:[NSString stringWithFormat:@"%zd",tag]];
            };
            
            TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:sexView];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
           
            break;
        case 3: // 修改生日
        {
            YTbirthdayView *birthView = [YTbirthdayView createViewFromNib];
            
            birthView.confirmButtonClick = ^(NSString *date) {
                [self postEditInfoWithIdx:indexPath value:date];
            };
            
            TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:birthView];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
            break;
        case 4: // 简介
        {
            TYAlertView *view = [TYAlertView alertViewWithTitle:@"修改简介" message:nil];
            
            __weak typeof(view) WeakView = view;
            view.titleLable.font = [UIFont systemFontOfSize:14];
            
            
            [view addAction:[TYAlertAction actionWithTitle:@"取消"
                                                     style:TYAlertActionStyleCancel
                                                   handler:nil]];
            
            [view addAction:[TYAlertAction actionWithTitle:@"确定"
                                                     style:TYAlertActionStyleDestructive
                                                   handler:^(TYAlertAction *action)
            {
                    [self postEditInfoWithIdx:indexPath value:[WeakView.textFieldArray.firstObject text]];
            }]];
            
            [view addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.placeholder = @"请输入内容";
            }];
            
            //            // first way to show
            TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
           
            break;
        case 5:
           
            break;
        default:
            break;
    }
    
    
}

- (void)postEditInfoWithIdx:(NSIndexPath *)idx value:(NSString *)value
{
    NSString *meta_key;
    switch (idx.row)
    {
        case 0: // 更换头像
            break;
        case 1: // 修改昵称
            meta_key = @"display_name";
            break;
        case 2: // 设置性别
            meta_key = @"sex";
            break;
        case 3:
            meta_key = @"birthday";
            break;
        case 4:
            meta_key = @"description";
            break;
        case 5:
            meta_key = @"user_phone";
            break;
        default:
            break;
    }

    [YTNetworkManager.shareManager postEditedMineInfoWithMeta_key:meta_key
                                                       meta_value:value
                                                       completion:^(BOOL isSucc, NSString *statusInfo)
     {
         if (!isSucc) {
             [self.view showTipWithTipStr:statusInfo];
             return ;
         }
         
         [self.view showTipWithTipStr:statusInfo];
         
         [self loadUserInfo];
     }];
}


@end
